package zara 
import org.codehaus.groovy.grails.plugins.springsecurity.Secured
@Secured(['ROLE_USER'])
class WebsiteController {
    def authenticateService
    static allowedMethods = [save: "POST", update: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        def person = authenticateService.userDomain()
        params.max = Math.min(params.max ? params.int('max') : 20, 100)
        [websiteInstanceList: Website.findAllByPerson(person,[max:params.max,offset:params.offset]), websiteInstanceTotal: Website.countByPerson(person)]
    }

    def create = {
        def websiteInstance = new Website()
        websiteInstance.properties = params
        return [websiteInstance: websiteInstance]
    }

    def save = {
        def websiteInstance = new Website(params)
        websiteInstance.person  = authenticateService.userDomain()
        if (websiteInstance.save(flush: true)) {
            User currentUser = User.get(authenticateService.userDomain().id)
            currentUser.siteTotal++
            currentUser.save(flush: true)
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'website.label', default: 'Website'), websiteInstance])}"
            redirect(action: "list")
        }
        else {
            render(view: "create", model: [websiteInstance: websiteInstance])
        }
    }

    def show = {
        def websiteInstance = Website.get(params.id)
        if (!websiteInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'website.label', default: 'Website'), params.id])}"
            redirect(action: "list")
        }
        else {
            [websiteInstance: websiteInstance]
        }
    }

    def edit = {
        def websiteInstance = Website.get(params.id)
        if (!websiteInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'website.label', default: 'Website'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [websiteInstance: websiteInstance]
        }
    }

    def update = {
        def websiteInstance = Website.get(params.id)
        if (websiteInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (websiteInstance.version > version) {
                    
                    websiteInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'website.label', default: 'Website')] as Object[], "Another user has updated this Website while you were editing")
                    render(view: "edit", model: [websiteInstance: websiteInstance])
                    return
                }
            }
            websiteInstance.properties = params
            if (!websiteInstance.hasErrors() && websiteInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'website.label', default: 'Website'), websiteInstance])}"
                redirect(action: "list")
            }
            else {
                render(view: "edit", model: [websiteInstance: websiteInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'website.label', default: 'Website'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def websiteInstance = Website.get(params.id)
        if (websiteInstance && websiteInstance.person.id == authenticateService.userDomain().id) {
            try {
                websiteInstance.delete(flush: true)
                User currentUser = User.get(authenticateService.userDomain().id)
                currentUser.siteTotal--
                currentUser.save(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'website.label', default: 'Website'), websiteInstance])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'website.label', default: 'Website'), websiteInstance])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'website.label', default: 'Website'), params.id])}"
            redirect(action: "list")
        }
    }
}
